var app = getApp()
var verify = require('../../../../utils/verify-token.js')
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		navData: [{
				text: '按货道查看',
        id:1
			},
			{
				text: '按商品查看',
        id: 2
			},
			{
				text: '补货记录',
        id: 3
			}
		],
		currentTab: 0,
		page:0,
		totalPage:0,
		loaded:false,
		loading:false,

	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var that = this
		var deviceId = options.deviceId;
		this.setData({
			sh: app.globalData.screenHeight,
			sw: app.globalData.screenWidth,
			deviceId: deviceId
		})
		wx.getSystemInfo({
			success: (res) => {
				this.setData({
					pixelRatio: res.pixelRatio,
					windowHeight: res.windowHeight,
					windowWidth: res.windowWidth
				})
			},
		})
		this.getListByAisle();

	},

	switchNav(event) {
		var cur = event.currentTarget.dataset.current;
		if (this.data.currentTab == cur) {
			return false;
		} else {			
			this.setData({
				currentTab: cur
			})
			if(cur==0){
				this.getListByAisle();
			}else if(cur==1){
				this.getCommodityList();
			}else if(cur==2){
				let that = this;		
				that.setData({
					ListData:[],
					page:0,
					totalPage:0			
				});
				that.replenishRecord();
			}
		}
	},
	//按照货道查看
	getListByAisle() {
		var thm = this;
		wx.request({
			url: app.globalData.baseUrl + 'vending/lib/' + thm.data.deviceId + '/goods_aisle/record',
			method: 'get',
			header: {
				'AUTHORIZATION': app.globalData.token
			},
			success: function(res) {
				if (res.data.code == 60006) {
					verify.clearAll()
				} else if (res.data.code == 200) {
					thm.setData({
						aisleListData: res.data.data
					})
				} else {
					wx.showModal({
						title: '操作失败',
						content: res.data.message,
					})
				}
			}
		})
	},
	//按照商品查看
	getCommodityList(){
		var thm = this;
		wx.request({
			url: app.globalData.baseUrl + 'vending/lib/' + thm.data.deviceId + '/commodity/record',
			method: 'get',
			header: {
				'AUTHORIZATION': app.globalData.token
			},
			success: function(res) {
				if (res.data.code == 60006) {
					verify.clearAll()
				} else if (res.data.code == 200) {
					thm.setData({
						commodityListData: res.data.data
					})
				} else {
					wx.showModal({
						title: '操作失败',
						content: res.data.message,
					})
				}
			}
		})
	},
	//补货记录
	replenishRecord(){
		let thm = this;
		let param = {				
			size:10,
			page:thm.data.page
		};
		wx.request({
		  url: app.globalData.baseUrl+'vending/lib/'+thm.data.deviceId+'/replenish/record',
		  method: 'get',
		  header:{
		    'AUTHORIZATION':app.globalData.token
		  },
		  data:param,
		  success: function (res) {	
		    if (res.data.code == 60006) {
		      verify.clearAll()
		    } else if (res.data.code == 200) {
				let listdata = res.data.data.content;		 
		        let listD = thm.data.ListData;
		        let ldh = listD.length;
		        for(let i=0;i<listdata.length;i++){
		          listD[ldh+i] = listdata[i]
		        }
			  
		        thm.setData({
		          ListData:listD,
		          totalPage:res.data.data.totalPages,
		          loading:res.data.data.last?false:true,
		          loaded:res.data.data.last?true:false
		        })
			 // console.log(thm.data.ListData);
		    } else {
		      wx.showModal({
		        title: '操作失败',
		        content: res.data.message,
		      })
		    }
		  }
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
	
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {
		let that=this;
		let page = that.data.page + 1; 
		if(page<=that.data.totalPage-1){	
			that.setData({
				page: page			
			})					
			that.replenishRecord();
		}else{
			that.setData({
				loaded:true,
				loading:false			
			})
		}
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
